package com.boot.adminservice.map.service.impl;

import com.boot.adminapi.map.constants.MapConstants;
import com.boot.adminservice.map.domain.dto.*;
import com.boot.adminservice.map.service.TencentMapProvider;
import com.boot.commondomain.domain.R;
import com.boot.commondomain.domain.ResultCode;
import com.boot.commondomain.exception.ServiceException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 地图服务的实现类
 */
@RefreshScope
@Component
@Data
@Slf4j
@ConditionalOnProperty(value = "map.type", havingValue = "tencentmap")
public class TencentMapServiceImpl implements TencentMapProvider {


    /**
     * 腾讯位置服务域名
     */
    @Value("${tencent.map.api}")
    private String api;

    /**
     * 调用腾讯位置服务的秘钥
     */
    @Value("${tencent.map.key}")
    private String key;

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 获取腾讯位置服务接口返回的全量区划数据持久化到mysql
     * @return 持久化是否成功
     */
    public TencentRegionResponseDTO saveRegionData() {
        // 1 构建请求url
        String url = String.format(
                api + MapConstants.TENCENT_MAP_API_PLACE_LIST +
                        "?key=%s",
                key
        );
        // 2 直接发送请求，并拿到返回结果再做对象转换
        ResponseEntity<TencentRegionResponseDTO> response =  restTemplate.getForEntity(url, TencentRegionResponseDTO.class);
        if (!response.getStatusCode().is2xxSuccessful()) {
            log.error("获取全量区划数据查询结果异常", response);
            throw new ServiceException(ResultCode.Tencent_MAP_QUERY_FAILED);
        }
        return response.getBody();
    }

    /**
     * 根据关键词搜索地点
     * @param suggestSearchDTO 搜索条件
     * @return 搜索结果
     */
    @Override
    public PoiListDTO searchTencentMapPlaceByRegion(SuggestSearchDTO suggestSearchDTO) {
        // 1 构建请求url
        String url = String.format(
                api + MapConstants.TENCENT_MAP_API_PLACE_SUGGESTION +
                        "?key=%s&region=%s&region_fix=1&page_index=%s&page_size=%s&keyword=%s",
                key, suggestSearchDTO.getId(), suggestSearchDTO.getPageIndex(), suggestSearchDTO.getPageSize(),suggestSearchDTO.getKeyword()
        );
        // 2 直接发送请求，并拿到返回结果再做对象转换
        ResponseEntity<PoiListDTO> response =  restTemplate.getForEntity(url, PoiListDTO.class);
        if (!response.getStatusCode().is2xxSuccessful()) {
            log.error("获取关键词查询结果异常", response);
            throw new ServiceException(ResultCode.Tencent_MAP_QUERY_FAILED);
        }
        return response.getBody();
    }

    /**
     * 根据经纬度来获取区域信息
     * @param locationDTO 经纬度
     * @return 区域信息
     */
    @Override
    public GeoResultDTO getTencentMapDistrictByLonLat(LocationDTO locationDTO) {
        // 1 构建请求url
        String url = String.format(api + MapConstants.TENCENT_MAP_GEOCODER +
                "?key=%s&location=%s",
                key, locationDTO.formatInfo()
        );
        // 2 直接发送请求，并拿到返回结果再做对象转换
        ResponseEntity<GeoResultDTO> response =  restTemplate.getForEntity(url, GeoResultDTO.class);
        if (!response.getStatusCode().is2xxSuccessful()) {
            log.error("根据经纬度来获取区域信息查询结果异常", response);
            throw new ServiceException(ResultCode.Tencent_MAP_QUERY_FAILED);
        }
        return response.getBody();
    }
}
